﻿Public Class FormGame
	Dim Game As New Game

	Public Timer As DisplayTimer


	Public Sub New()

		' This call is required by the designer.
		InitializeComponent()

		' Add any initialization after the InitializeComponent() call.

		Timer = New DisplayTimer(AddressOf UpdateMap, 50)
	End Sub

	Public Delegate Sub UpdateMapDelegate()
	Public Sub UpdateMap()
		If Me.InvokeRequired Then
			Me.Invoke(New UpdateMapDelegate(AddressOf UpdateMap))
		Else
			If Game IsNot Nothing Then
				If Game.CurrentMap IsNot Nothing Then
					Game.Update()
					MapImage.Image = Game.CurrentMap.BitMap
				End If
			End If

			Update()
		End If
	End Sub

	Private Sub MapEditorToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MapEditorToolStripMenuItem.Click
		Dim editor As New FormMapEditor
		editor.Show()


	End Sub

	Private Sub MapEditor2ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MapEditor2ToolStripMenuItem.Click

		Dim editor As New FormMapEditor2
		editor.Show()


	End Sub
	Private Sub NewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewToolStripMenuItem.Click
		Timer.StopTask()
		Game = New Game
		Game.LoadGame()
		Timer.StartTask()
	End Sub

	Private Sub FormGame_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
		If Game Is Nothing Then
			Return
		End If
		If Game.CurrentMap Is Nothing Then
			Return
		End If
		Dim LocX As Integer = Game.Player.LocX
		Dim LocY As Integer = Game.Player.LocY
		If e.KeyChar = "w" Then
			LocY = Math.Max(Game.Player.LocY - 1, 0)
		ElseIf e.KeyChar = "s" Then
			LocY = Math.Min(Game.Player.LocY + 1, Game.CurrentMap.Height - 1)
		ElseIf e.KeyChar = "a" Then
			LocX = Math.Max(Game.Player.LocX - 1, 0)
		ElseIf e.KeyChar = "d" Then
			LocX = Math.Min(Game.Player.LocX + 1, Game.CurrentMap.Width - 1)
		End If
		If Not Game.CurrentMap.MapTileMatrix(LocX)(LocY).Solid Then
			Game.Player.LocX = LocX
			Game.Player.LocY = LocY
		End If
	End Sub

End Class
